package com.alks.function.controller;

import com.alks.common.aop.Idempotent;
import com.alks.common.ratelimit.annotation.RateLimit;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.EmailSendRequest;
import com.alks.function.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/open/email")
public class EmailController {

    @Autowired
    private IEmailService emailService;

    @PostMapping("/send")
    @Idempotent(expireTime = 30, info = "Repeat request.")
    @RateLimit(key = "email_send_ip", limit = 3, timeout = 60, message = "Request is too frequent, please try again later.")
    public ResponseInfo sendEmail(@RequestBody EmailSendRequest request) {
        try {
            emailService.sendEmail(request);
            return ResponseInfo.ok("Email sent successfully!");
        } catch (Exception e) {
            return ResponseInfo.error("Error sending email: " + e.getMessage());
        }
    }
}